<?php
namespace app\agent\controller;
use think\Controller;
use think\request;
use think\Db;


class LoginController extends controller
{
    public function index()
    {
        return  $this->fetch();
    }
    
    
    public function login() {
		if (session('agent_id')) {
            $this->redirect('agent/index/index');
        }
		if(request()->isPost()){
			$where['mobile'] = input('mobile');
			$where['password'] = md5_salt(input('password'));
			$agent_info = select_data('agent',$where);
			if($agent_info['status'] == 2){
				$status='帐号冻结';
				return json(['code'=>402,'msg'=>$status]);
			}
			if($agent_info){
				session('agent_id', $agent_info['id']);
				session('agent_name', $agent_info['agent_name']);
				$status='登录成功';
				return json(['code'=>200,'msg'=>$status]);
			}else{
				$status='账户名或密码错误';
            	return json(['code'=>402,'msg'=>$status]);
			}
		}
		
		return $this->fetch();
	}
  	
  	/**
     * 修改密码
     */
    public function forget() {
    	if(request()->isPost()){
    		
    		$result = check_sendcode($_POST['mobile'], $_POST['yzm'], '发送成功');
			if($result != 1) {
				return json(['code'=>405,'msg'=>'验证码错误或过期']);
			}
    		$agent_info = db('agent')->where('mobile',$_POST['mobile'])->find();
	    	if($agent_info){
	    			$pwd = md5_salt($_POST['password']);
	    			$result = db('agent')->where('id', $agent_info['id'])->update(['password' => $pwd]);
	    			if($result){
	    				return json(['code'=>200,'msg'=>'修改成功']);
	    			}else{
	    				return json(['code'=>402,'msg'=>'修改失败']);
	    			}
	   		}else{
	   			return json(['code'=>403,'msg'=>'用户不存在']);
	   		}
    	}
    	return  $this->fetch();
    } 
    
    
    public function about() {
        return  $this->fetch();
    } 
  	/**
     * 退出登录
     */
    public function logout() {
        session(null);
        return json(['code'=>200,'msg'=>'退出成功']);
    } 
}
